﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Dynamic;
using SDT.Lib.Common;
using SDT.Model;
using SDT.Admin.Properties;
using SDT.Admin.ViewModels;
using SDT.Lib.Paging;
using System.Web.UI.WebControls;
using SDT.Lib.Helpers;
using MVCMessage;
namespace SDT.Admin.Controllers
{
    /// <summary>
    /// Author: Takamaru Nguyen
    /// Date: 13/06/2011
    /// </summary>
	[RoleHelper(Roles = RoleSystem.Dictionary)]
    public class ProductCategoryController : BaseController
    {
        SDTDataContext eDataContext = new SDTDataContext();
        //
        // GET: /ProductCategory/
        public ActionResult Index(int? page)
        {
            try
            {
                if (page == null) page = 1;
                var model = new ProductCategoryIndexViewModels();
                string order = UserState.getUserStateFromRequest("productCategory.order", "order", "ID").ToString();
                string orderDir = UserState.getUserStateFromRequest("productCategory.orderDir", "orderDir", "desc").ToString();
                model.Keyword = UserState.getUserStateFromRequest("productCategory.Keyword", "Keyword", "").ToString();
                int pageSize = int.Parse(UserState.getUserStateFromRequest("productCategory.pageSize", "PageSize", 20).ToString());
                
                model.Categories = eDataContext.ProductCategories.Where(e => (string.IsNullOrEmpty(model.Keyword) ? true : e.Name.ToLower().Contains(model.Keyword.ToLower().Trim())))
                                    .OrderBy(string.Format("{0} {1}", order, orderDir))
                                    .ToPagedList(page.Value - 1, pageSize);

                    ViewData["orderDir"] = orderDir;
                    ViewData["order"] = order;
                    return View(model);
            }
            catch (Exception exp)
            {
                Messages.AddErrorFlashMessage(exp.Message);
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
        }

        //
        // GET: /ProductCategory/Details/5
        //public ActionResult Details(int id)
        //{
        //    try
        //    {
        //            ProductCategory proCat = eDataContext.ProductCategories.Where(c => c.ID == id).SingleOrDefault();
        //            List<SelectListItem> listT = eDataContext.ProductTypes
        //                                        .Select(t => new SelectListItem()
        //                                        {
        //                                            Text = t.Name,
        //                                            Value = t.ID.ToString()
        //                                        }).ToList();
        //            List<TreeNodeItem> listTree = eDataContext.ProductCategories
        //                                            .Where(c => c.ID != proCat.ID)
        //                                            .Select(c => new TreeNodeItem()
        //                                            {
        //                                                Value = c.ID,
        //                                                Text = c.Name,
        //                                                ParentID = c.ParentID
        //                                            }).ToList();
        //            TreeNodeCollection categories = new BuiltTreeCollection().BuiltTreeNodeCollection(listTree, proCat.ParentID);

        //            return View(new ProductCategoryEditModels
        //            {
        //                ListParentCategories = categories,
        //                Category = proCat,
        //                ListProductType = listT
        //            });
        //    }
        //    catch (Exception exp)
        //    {
        //        ViewData[Messages.MessageInfo] = exp.Message;
        //        return View();
        //    }
        //}

        //
        #region  GET: /ProductCategory/Create
        public ActionResult Create()
        {
            try
            {
                ProductCatEditViewModel model = new ProductCatEditViewModel();
                    return View(model);
            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
        }
    #endregion
        
        // POST: /ProductCategory/Create

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(ProductCatEditViewModel model)
        {
            try
            {
                    model.ProductCategory.AddBy = UserManager.GetUserId();
                    model.ProductCategory.AddDate = DateTime.Now;
                    eDataContext.ProductCategories.InsertOnSubmit(model.ProductCategory);
                    eDataContext.SubmitChanges();
                    Messages.AddFlashMessage("Thêm chuyên mục '"+model.ProductCategory.Name +"' thành công!");
                    return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                Messages.AddErrorFlashMessage("Thêm chuyên mục không thành công");
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
        }

        
        //  GET: /ProductCategory/Edit/5
        public ActionResult Edit(int id)
        {
            try
            {
                ProductCatEditViewModel model = new ProductCatEditViewModel();
                    model.ProductCategory = eDataContext.ProductCategories.Where(c => c.ID == id).SingleOrDefault();
                    return View(model);
            }
            catch (Exception exp)
            {
                Messages.AddErrorFlashMessage("Hệ thống gặp sự cố. Bạn không thể sửa được chuyên mục này");
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
        }

        
        // POST: /ProductCategory/Edit/5

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int id, ProductCatEditViewModel model)
        {
            try
            {
                model.ProductCategory = eDataContext.ProductCategories.Where(c => c.ID == id).SingleOrDefault();
                UpdateModel(model);
                model.ProductCategory.ModifyBy = UserManager.GetUserId();
                model.ProductCategory.ModifyDate = DateTime.Now;
                eDataContext.SubmitChanges();
                Messages.AddFlashMessage("Sửa thông tin của chuyên mục '"+model.ProductCategory.Name+"' thành công");
                return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                Messages.AddErrorFlashMessage("Sửa thông tin chuyên mục không thành công");
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
        }

        //
        // GET: /Article/Delete/5
        public ActionResult Delete(int id)
        {
            try
            {
                    
                    ProductCategory proCat = eDataContext.ProductCategories.SingleOrDefault(e => e.ID == id);
                    
                    return View(proCat);
            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
        }

        //
        // POST: /Article/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                    ProductCategory item = eDataContext.ProductCategories.SingleOrDefault(e => e.ID == id);
                    if (item != null)
                    {
                        eDataContext.ProductCategories.DeleteOnSubmit(item);
                        eDataContext.SubmitChanges();
                    }
                    Messages.AddFlashMessage("Xóa chuyen mục thành công");
                    return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
        }
    }
}
